package com.xb.loan.token.factory.monitor;

import com.xb.loan.token.factory.utils.EhcacheUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DecimalFormat;
import java.util.TimerTask;

/**
 * User: erin
 * Date: 15/4/29
 * Time: 下午1:51
 */
public class ShootingMonitor extends TimerTask {

    private static final Logger logger = LoggerFactory.getLogger("tokenLocalCacheLogger");

    private long hitsLastMin;

    private long missesLastMin = 0;

    @Override
    public void run() {
        long hits = EhcacheUtil.getCacheHits();
        long misses = EhcacheUtil.getCacheMisses();
        long hitsMin = hits - hitsLastMin;
        hitsLastMin = hits;
        long misMin = misses - missesLastMin;
        missesLastMin = misses;
        long countMin = hitsMin + misMin;
        double shooting;
        String per = null;
        if (countMin > 0) {
            shooting = hitsMin * 1.0 / (hitsMin + misMin);
            DecimalFormat df = new DecimalFormat();
            df.applyPattern("#0.00%");
            per = df.format(shooting);
        }
        logger.info("[token] local cache hits:" + hitsMin + ",misses=" + misMin + ",shooting=" + per);
    }

    public static void main(String[] args) {
        long hitsMin = 26000;
        long misMin = 9999911;
        double shooting;
        String per;
        shooting = hitsMin * 1.0 / (hitsMin + misMin);
        DecimalFormat df = new DecimalFormat();
        df.applyPattern("#0.00%");
        per = df.format(shooting);
//        System.out.println(per);
    }
}
